package com.zhangtai.modules.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.zhangtai.modules.controller.aouthLogin.LoginController;
import com.zhangtai.modules.dto.CertificateDto;
import com.zhangtai.modules.entity.certificate.CertificateEntity;
import com.zhangtai.modules.entity.contract.ContractModelEntity;
import com.zhangtai.modules.entity.employee.EmployeeInfoEntity;
import com.zhangtai.modules.entity.employee.EmployerCompanyEntity;
import com.zhangtai.modules.service.baseFromMssql.BaseMssqlServiceImpl;
import com.zhangtai.modules.service.certificate.CertificateService;
import com.zhangtai.modules.service.contract.ContractModelService;
import com.zhangtai.modules.service.employee.EmployeeInfoService;
import com.zhangtai.modules.service.employee.EmployerCompanyService;
import com.zhangtai.modules.vo.CertificateListVo;
import com.zhangtai.modules.vo.CertificateVo;
import com.zhangtai.utils.BeanUtilsMabach;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("cer")
@Api(tags = "证明开据")
public class CertificateController {
    @Autowired
    private CertificateService certificateService;
    @Autowired
    private LoginController loginController;
    @Autowired
    private EmployerCompanyService employerCompanyService;
    @Autowired
    private ContractModelService contractModelService;
    @Autowired
    private EmployeeInfoService employeeInfoService;
    @Autowired
    private BaseMssqlServiceImpl baseMssqlService;
    @Autowired
    private JobController jobController;

    @PostMapping("/saveOrUpdateCertificateInfo")
    @ApiOperation("保存/修改证明信息")
    public R saveOrUpdateCertificateInfo(@RequestBody CertificateVo vo, HttpServletRequest request)throws Exception{
        JSONObject userInfo  = loginController.getUserInfo(request).getData();
        if(ObjectUtils.isEmpty(userInfo)){
            return R.error("用户信息不存在！");
        }
        String code = userInfo.getString("usercode");
        CertificateEntity entity = new CertificateEntity();
        EmployerCompanyEntity companyEntity  = employerCompanyService.getByIdE(vo.getCompanyId());
        ContractModelEntity modelEntity = contractModelService.getByIdE(vo.getModelId());
        EmployeeInfoEntity infoEntity = employeeInfoService.getByIdE(vo.getOid());
        entity.setApplierCode(code);
        entity.setCompanyId(vo.getCompanyId());
        entity.setCompany(companyEntity.getName());
        entity.setModelId(vo.getModelId());
        entity.setModelName(modelEntity.getName());
        entity.setOid(vo.getOid());
        entity.setUnitId(infoEntity.getUnitId());
        entity.setJobId(infoEntity.getJobId());
        entity.setCode(infoEntity.getCode());
        entity.setCreateTime(new Date());
        entity.setPosition(vo.getPosition());
        entity.setName(infoEntity.getName());
        entity.setOrgId(infoEntity.getOrgId());
        if(!ObjectUtils.isEmpty(vo.getId())){
            entity.setId(vo.getId());
            certificateService.updateByIdE(entity);
        }else{
            certificateService.saveE(entity);
        }
        return R.ok();
    }

    @DeleteMapping("/deleteCertificate")
    @ApiOperation("删除证明信息")
    public R deleteCertificate(String id){
        certificateService.removeOneById(Long.parseLong(id));
        return R.ok();
    }


    @PostMapping("/getCertificateList")
    @ApiOperation("获取开据证明列表")
    public R<PageResult<CertificateDto>> getCertificateList(@RequestBody CertificateListVo vo, HttpServletRequest request)throws Exception{
        List<Long> jobIds = null;
        List<Long>  unitIds = null;
        //获取当前用户角色下公司Id
        R checkR = loginController.checkOrgPermission(request,null,false);
        if(!R.isOk.equals(checkR.getStatus())){
            return checkR;
        }
        List<Long> orgIds = (List<Long>) checkR.getData();
        //部门
        if(!StringUtil.isNullOrEmpty(vo.getUnitName())){
            unitIds = baseMssqlService.finOrgByOrgName(vo.getUnitName());
        }
        //职位
        if(!StringUtil.isNullOrEmpty(vo.getJobName())){
            jobIds = baseMssqlService.findJobIdByJobName(vo.getJobName());
        }

        PageResult<CertificateEntity> result = certificateService.getCertificateList(vo.getCode(),vo.getName(),unitIds,orgIds,
                jobIds,vo.getStartDate(),vo.getEndDate(),vo.getModelId(),vo.getStatus(),null,vo.getPageNum(),vo.getPageSize());
        List<CertificateEntity> list = result.getList();
        List<CertificateDto> dtoList = new ArrayList<>();
        for(CertificateEntity entity:list){
            CertificateDto dto = BeanUtilsMabach.doToDto(entity,CertificateDto.class);
            dto.setJobName(jobController.jobMap.get(entity.getJobId()));
            dto.setUnitName(jobController.orgMap.get(entity.getUnitId()));
            dto.setOrgName(jobController.orgMap.get(entity.getOrgId()));
            dtoList.add(dto);
        }
        PageResult<CertificateDto> result1 = new PageResult<>();
        result1.setTotalPage(result.getTotalPage());
        result1.setTotalCount(result.getTotalCount());
        result1.setPageSize(result.getPageSize());
        result1.setCurrPage(result.getCurrPage());
        result1.setList(dtoList);
        return R.ok(result1);
    }


}
